package com.esdk.utils;


import java.sql.Connection;
import java.util.Date;
import java.util.Properties;

import com.esdk.esdk;
import com.esdk.sql.ConnectionBuilder;
import com.esdk.sql.orm.tools.ORMCreater;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;


public class ORMGenerator{
	static Log log=LogFactory.get();
	
	/** 数据库备份 
	 * mysqldump -u sys --password=aha801801 -R stock -h db.server -P 3306 | gzip >/datadisk/db_backup/stock_20210323.sql.zip
	 * */
	private static Runnable getDBBackupRunable(){
		Properties props=esdk.file.getProperties("db.properties");
		String url=props.getProperty("jdbc.url");
		String[] subs=EasyRegex.findSub(url,"^.*?//(.*?):?(\\d*)/([\\w-_]*)(\\?.*)");
		final String host=subs[0], port=subs[1], dbname=subs[2];
		String username=props.getProperty("jdbc.username");
		String password=props.getProperty("jdbc.password");
		return new Runnable(){
			public void run(){
				log.info("正在备份数据库{}...",dbname);
				try{
					String backupPath="/datadisk/db_backup/";
					EasyFile.createFolder(backupPath,true);
					String filepath=EasyStr.concat(backupPath,dbname,"_",EasyTime.format(new Date(),esdk.time.COMPACTDATE_FORMAT),".sql");
					String dumpcmd=EasyStr.format("mysqldump -u {0} --password={1} -R {2} -h {3} -P {4} | gzip >{5}.zip",username,password,dbname,host,port,filepath);
					esdk.tool.exec(dumpcmd);
					// 删除多余的备份文件
					DeleteRedundancyFile.exec(backupPath, dbname); 
					log.info("已完成数据库备份");
				}catch(Exception e){
					log.error(e);
				}
			}
		};
	}
	
	private static void makeORM() throws Exception{
		Connection conn=ConnectionBuilder.createConnection("db.properties");
//		String[] tables=esdk.sql.getTablesAndViews(conn.getMetaData());
		String[] tables="me_contractliability_order".split(",| +");
//	String[] tables=new JDBCTemplate(con,"SELECT table_name FROM user_tables union SELECT view_name as table_name FROM User_Views").toABResultSet().getStrings("TABLE_NAME"); //for oracle
		String targetPath="src/test/java/";
		String ormPackagePath=esdk.prop.get("esdk_orm_package_name","com.esdk.test.orm");
//		ORMCreater.isCreateBean=false;
//		BeanCodeCreater.isCreateSwaggerAnnotation=false;
		ORMCreater.make(conn,ormPackagePath,tables,targetPath,true,esdk.file.getInputStream("orm_relationship.xml"));
	}
	
	private static void generateDF() throws Exception{
//		Connection conn=FileConnectionPool.getConnection();
//		String[] tables=esdk.sql.getTablesAndViews(conn.getMetaData());
		String[] tables=new String[0];
		String ormPackagePath="com.esdk.test.orm";
		String targetPath="src/test/java/com/esdk/test/orm/";
		ModulesCodeGenerator gentor=new ModulesCodeGenerator(ormPackagePath,null,targetPath);
		gentor.setGenerateController(false);
		gentor.setGenerateService(false);
		gentor.setGenerateUploadService(false);
		gentor.setGenerateVuePage(false);
		gentor.generateFiles(tables);
	}

	public static void main(String[] args) throws Exception{
		makeORM();
//		generateDF();
//		getDBBackupRunable().run();
	}


}
